﻿using System.Windows;
using IDesktop.Core;

namespace IDesktop.View
{
    /// <summary>
    /// FistRun.xaml 的交互逻辑
    /// </summary>
    public partial class SetupWindow : Window
    {
        private bool _isSelected = false;
        private bool _neverShow = false;
        private string _cityName = string.Empty;
        private string _cityCode = string.Empty;
        private string _savePath = string.Empty;

        public delegate void RefreshConfigEventHandler();
        public event RefreshConfigEventHandler EventHandler;

        public SetupWindow()
        {
            InitializeComponent();
            //selectCity.Path = Config.Read("Path");
            //selectCity.Path = AppDomain.CurrentDomain.BaseDirectory + "/Resource/Citys.xml";
            selectCity.CityChangedEvent += new SelectCity.CityChangedEventHandler(SelectCity_CityChangedEvent);
        }

        private void Submit_Click(object sender, RoutedEventArgs e)
        {
            if (!_isSelected)
            {
                MessageBox.Show("请选择城市!");
            }
            else
            {
                Config.Write("CityName", _cityName);
                Config.Write("CityCode", _cityCode);
                Config.Write("SavePath", _savePath);

                if (neverShow.IsChecked == true)
                {
                    _neverShow = true;
                }
                Config.Write("NeverShow", _neverShow.ToString());
                if (EventHandler != null)
                {
                    EventHandler();
                }
                this.Close();
            }
        }

        private void SelectCity_CityChangedEvent(object sender, CityChangedEventArgs e)
        {
            _isSelected = true;
            _cityCode = e.ID;
            _cityName = e.Name;
        }

        private void SelectPath_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog fbd = new System.Windows.Forms.FolderBrowserDialog();
            if (System.Windows.Forms.DialogResult.OK == fbd.ShowDialog())
            {
                _savePath = fbd.SelectedPath;
                savePath_TextBlock.Text = _savePath;
            }
        }
    }
}
